package com.example.meizu002.service.impl;

import com.example.meizu002.entity.User;
import com.example.meizu002.mapper.UserMapper;
import com.example.meizu002.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.Date;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public void register(User user) {
        User userDB = userMapper.findByPhone(user.getPhone());
        if (userDB == null){
            //设置注册时间
            user.setRegisterTime(new Date());
            //调用Mapper
            userMapper.save(user);
        }else {
            throw new RuntimeException("用户名已存在！");
        }
    }

    @Override
    public User login(User user) {
        //根据用户输入电话号码进行查询
        User userDB = userMapper.findByPhone(user.getPhone());
        if (!ObjectUtils.isEmpty(userDB)){
            //比较密码
            if (userDB.getPassword().equals(user.getPassword())){
                return userDB;
            }else {
                throw new RuntimeException("密码不正确！");
            }
        }else {
            throw new RuntimeException("用户名输入错误！");
        }
    }
}
